package org.me.hello;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JApplet;

public class GameboardApplet extends Applet implements KeyListener, MouseListener {

    private static int BIG_IMAGE_SIZE = 96;
    private static int SMALL_IMAGE_SIZE = 50;
    private static int SCREEN_WIDTH = 600;
    private static int SCREEN_HEIGHT = 600;
    private static int MOVEMENT = 5;
    private static int AVAILABLE_MOVES = 4;

    private JPanel jContentPane = null;
    private int x;
    private int y;
    private Image minelayerShipImg;
    private Image minesweeperShipImg;
    private Image oceanImage;

    //Minador
    private MinelayerShip minelayerShip;
    //Barreminas
    private MinesweeperShip minesweeperShip;
    
    //Turno
    private Boolean minesweeperShipTurn;
    private Boolean minelayerShipTurn;
    
    //Cantidad de movidas por turno
    private int amountMinelayerMoves;
    private int amountMinesweeperMoves;
    
    public GameboardApplet() {
        super();        
        this.init();
    }

    public void init() {
        System.out.println("Hace el init");
        this.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        //this.setContentPane(getJContentPane());
        this.setDefaultValues();
        addKeyListener(this);
        addMouseListener(this);
        setFocusable(true);
        this.minelayerShip = new MinelayerShip(200, 200);
        this.minesweeperShip = new MinesweeperShip(10, 10);
        this.minesweeperShipTurn = false;
        this.minelayerShipTurn = true;
        this.amountMinelayerMoves = 0;
        this.amountMinesweeperMoves = 0;

    }

    public void setDefaultValues() {
        
       //minelayerShipImg = ImageIO.read(new URL(getCodeBase(), "images/battleship2.png"));
       URL minelayerUrl = getClass().getClassLoader().getResource("images/battleship2.png");
       minelayerShipImg = getToolkit().getImage(minelayerUrl);

       URL minesweeperUrl = getClass().getClassLoader().getResource("images/minesweeperShip.png");
       minesweeperShipImg = getToolkit().getImage(minesweeperUrl);

       URL oceanUrl = getClass().getClassLoader().getResource("images/ocean.jpg");
       oceanImage = getToolkit().getImage(oceanUrl);

    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
                jContentPane = new JPanel();
                jContentPane.setLayout(new BorderLayout());

        }
        return jContentPane;
    }

     public void paint(Graphics g){
        super.paint(g);
        //Agregar la imagen del oceano
        g.drawImage(oceanImage, 0, 0, this);
        //Agregar la imagen del barco minador en las coordenadas seteadas a partir de las variables.
        g.drawImage(minelayerShipImg, minelayerShip.getX(), minelayerShip.getY(), this);
        //Agregar la imagen del barco barreminas en las coordenadas seteadas a partir de las variables.
        g.drawImage(minesweeperShipImg, minesweeperShip.getX(), minesweeperShip.getY(), this);
       
       //Image myImage = Toolkit.getDefaultToolkit().getImage("./images/battleship.jpg");

        //Dibujar la cuadricula
        /*Graphics2D g2 = (Graphics2D) g;
        int count = 4;
        int size = 30;
        for( int i = 0; i < count; i ++)
        {
            for( int j = 0; j < count; j++)
            {
                Rectangle grid = new Rectangle( 240 + i * size, 240 + j * size, size, size);   
                g2.draw(grid);

            }

        }*/
     }

    @Override
    public void keyPressed(KeyEvent event) {

        if(this.minesweeperShipTurn){
            this.amountMinesweeperMoves += 1;
        } else {
            this.amountMinelayerMoves += 1;
        }
        //Dependiendo la tecla que apreta, incrementar o decrementar coordenadas.
        switch (event.getKeyCode()) {
                
            case 37://LEFT KEY
                if(this.minelayerShipTurn){
                    this.minelayerShip.setPos(this.minelayerShip.getX() - MOVEMENT, this.minelayerShip.getY());
                } else {
                    this.minesweeperShip.setPos(this.minesweeperShip.getX() - MOVEMENT, this.minesweeperShip.getY());
                }
                    
            break;
            case 38://UP KEY
                 if(this.minelayerShipTurn){
                    this.minelayerShip.setPos(this.minelayerShip.getX(), this.minelayerShip.getY() - MOVEMENT);
                } else {
                    this.minesweeperShip.setPos(this.minesweeperShip.getX(), this.minesweeperShip.getY() - MOVEMENT);
                }
            break;
            case 39://RIGHT KEY
                //this.x += MOVEMENT;
                if(this.minelayerShipTurn){
                    this.minelayerShip.setPos(this.minelayerShip.getX() + MOVEMENT, this.minelayerShip.getY());
                } else {
                    this.minesweeperShip.setPos(this.minesweeperShip.getX() + MOVEMENT, this.minesweeperShip.getY());
                }
            break;
            case 40://DOWN KEY
                if(this.minelayerShipTurn){
                    this.minelayerShip.setPos(this.minelayerShip.getX(), this.minelayerShip.getY() + MOVEMENT);
                } else {
                    this.minesweeperShip.setPos(this.minesweeperShip.getX(), this.minesweeperShip.getY() + MOVEMENT);
                }
            break;
        }
       //Cambio de turno si es necesario.
        this.changeTurn();
        
        // El rapaint llama al metodo paint que es el que dibuja la pantalla.
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        //Cuando hace click, posicionar el barco en la posición clickeada.
        this.x = e.getX();
        this.y = e.getY();
        
       
        //Incrementar la cantidad de movidas
        /*if(this.minesweeperShipTurn){
            this.amountMinesweeperMoves += 1;
        } else {
            this.amountMinelayerMoves += 1;
        }
        
        if(this.minelayerShipTurn){
            minelayerShip.setPos(this.x  - (SMALL_IMAGE_SIZE / 2), this.y  - (SMALL_IMAGE_SIZE / 2));
        } else {
            minesweeperShip.setPos(this.x  - (BIG_IMAGE_SIZE / 2), this.y  - (BIG_IMAGE_SIZE / 2));
        }
        
        this.changeTurn();*/
        
        //int newX = moverMinadorX(this.x);
        //int newY = moverMinadorY(this.y);
        
        System.out.println("Posicion original X: " + this.x);
        System.out.println("Posicion original Y: " + this.y);
        
        /*System.out.println("Posicion Nueva X: " + newX);
        System.out.println("Posicion Nueva Y: " + newY);
        
        
        minelayerShip.setPos(newX, newY);*/
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
    }
    
    public void changeTurn() {
        if(this.minelayerShipTurn && this.amountMinelayerMoves == AVAILABLE_MOVES) {
            this.minesweeperShipTurn = true;
            this.minelayerShipTurn = false;
            this.amountMinelayerMoves = 0;
        } else if(this.minesweeperShipTurn && this.amountMinesweeperMoves == AVAILABLE_MOVES){
            this.minelayerShipTurn = true;
            this.minesweeperShipTurn = false;
            this.amountMinesweeperMoves = 0;
        }
    }

/*    private static int moverMinadorY(int arg0) {
        client.BarcoService service = new client.BarcoService();
        client.Barco port = service.getBarcoPort();
        return port.moverMinadorY(arg0);
    }

    private static int moverMinadorX(int arg0) {
        client.BarcoService service = new client.BarcoService();
        client.Barco port = service.getBarcoPort();
        return port.moverMinadorX(arg0);
    }
 */  
}
